﻿using System;
using System.Xml;
using System.Windows.Forms;
//using System.Text.RegularExpressions;
using Notepad_X.Components;
namespace Notepad_X.Classes
{
    public class Toolbar
    {
        public string file;
        public string filename;
        private Main main;
        public string Name;
        public string Author;
        public string Description;
        public string Version;
        private string ImageFolder;
        public int DisplayIndex;
        /// <summary>
        /// Toolbar Control
        /// </summary>
        public ToolStrip nToolbar = new ToolStrip();

        public Toolbar()
        {
            main = Notepad_X.Static.MainInstance;
            DisplayIndex = 0;
        }
        /// <summary>
        /// Sets the image for the control
        /// </summary>
        /// <param name="item">The control instance</param>
        /// <param name="code">The image code</param>
        private void addimage(ToolStripItem item, string code)
        {
            if ( !String.IsNullOrEmpty(code))
            {

                if (code.Contains("|"))
                {
                    string[] decode = code.Split('|');
                    if (decode[0].ToLower() == "system")
                    {
                        string value = decode[1];
                        if (item.GetType() == typeof(MenuItemX))
                        {
                            //((MenuItemX)item).IconKey = value.ToLower();
                            ((MenuItemX)item).Icon = (Icons)Enum.Parse(typeof(Icons), value.ToLower());
                        }
                        else if (item.GetType() == typeof(ToolBarButtonX))
                        {
                            ((ToolBarButtonX)item).Icon = (Icons)Enum.Parse(typeof(Icons), value.ToLower());
                        }
                        else if (item.GetType() == typeof(ToolBarLabelX))
                        {
                            ((ToolBarLabelX)item).Icon = (Icons)Enum.Parse(typeof(Icons), value.ToLower());
                        }

                        string icon = value.ToLower();
                        if (main.IconsCollection.Images.ContainsKey(icon))
                        {
                            item.Image = main.IconsCollection.Images[icon];
                        }else{
                            item.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                        }
                           
                    }
                }
                else
                {
                    String path = new System.IO.FileInfo(file).Directory.ToString();

                    if (!path.EndsWith("\\"))
                    {
                        path += "\\";
                    }

                    if (code.ToLower().Contains("{folder}"))
                    {
                        item.Image = System.Drawing.Image.FromFile(path + ImageFolder + "\\" + code.Replace("{folder}", ""));
                    }
                    else
                    {
                        item.Image = System.Drawing.Image.FromFile(path + code);
                    }
                }
            }
        }
        /// <summary>
        /// Sets the text for the control
        /// </summary>
        /// <param name="item">The control instance</param>
        /// <param name="code">The text</param>
        private void addtext(ToolStripItem item, string code)
        {
            item.Text = code;
       
                if (code.Contains("|"))
                {
                    string[] decode = code.Split('|');
                    if (decode[0].ToLower() == "system")
                    {
                    if (item.GetType() == typeof(MenuItemX))
                    {
                        ((MenuItemX)item).String = (Strings)Enum.Parse(typeof(Strings), decode[1], true);
                    }
                    else if (item.GetType() == typeof(ToolBarButtonX))
                    {
                        ((ToolBarButtonX)item).String = (Strings)Enum.Parse(typeof(Strings), decode[1], true);
                    }
                    else if (item.GetType() == typeof(ToolBarLabelX))
                    {
                        ((ToolBarLabelX)item).String = (Strings)Enum.Parse(typeof(Strings), decode[1], true);
                    }
                    }
                }else{
                    item.Text = code;
                    item.ToolTipText = code;
                }
        }

        /// <summary>
        /// Adds Click event handlers to controls
        /// </summary>
        /// <param name="item">The control instance</param>
        /// <param name="code">The command</param>
        private void addcode(ToolStripItem item, string code)
        {
            if (code.Contains("|"))
            {
                string[] decode = code.Split('|');
                string value = decode[1];
                if (decode[0].ToLower() == "system")
                {
                    item.Click += Notepad_X.Classes.Event.Events.AddHandler(value);
                }
                else if (decode[0].ToLower() == "tag")
                {
                    if (decode.Length == 2)
                    {
                        item.Tag = decode[1];
                    }else if (decode.Length == 3){
                        item.Tag = decode[1] + "|" + decode[2];
                    }
                    item.Click += new EventHandler(this.main.handler_tag);
                }
            }
        }
        /// <summary>
        /// Remove this toolbar from the application
        /// </summary>
        public void Delete()
        { 
            main.Controls.Remove(nToolbar);
            nToolbar = null;
        }
        /// <summary>
        /// Creates the toolbar from the toolbar file
        /// </summary>
        /// <param name="filePath">The toolbar file path</param>
        public void Create(string filePath)
        {
            file = filePath;
            filename = System.IO.Path.GetFileName(file);
            XmlDocument xmlFile = new XmlDocument();
            xmlFile.Load(file);

            Name = xmlFile.SelectSingleNode("Toolbar/Information/Name").InnerText;
            Author = xmlFile.SelectSingleNode("Toolbar/Information/Author").InnerText;
            Version = xmlFile.SelectSingleNode("Toolbar/Information/Version").InnerText;
            Description = xmlFile.SelectSingleNode("Toolbar/Information/Description").InnerText;

            if (xmlFile.SelectNodes("Toolbar/Information/ImageFolder").Count != 0)
            {
                ImageFolder = xmlFile.SelectSingleNode("Toolbar/Information/ImageFolder").InnerText;
            }

            XmlNodeList nodes = xmlFile.SelectNodes("Toolbar/Item");

            nToolbar.BringToFront();
            nToolbar.GripStyle = ToolStripGripStyle.Hidden;
            nToolbar.Dock = DockStyle.Top;
            nToolbar.ShowItemToolTips = true;
            nToolbar.RenderMode = ToolStripRenderMode.Professional;
            if (nodes.Count != 0)
            {
                foreach (XmlNode node in nodes)
                {
                    switch (node.SelectSingleNode("Type").InnerText.ToLower())
                    {
                        case "button":
                            ToolBarButtonX nbutton = new ToolBarButtonX();
                            nToolbar.Items.Add(nbutton);
                            if (node.SelectNodes("Code").Count != 0)
                            {
                                addcode(nbutton, node.SelectSingleNode("Code").InnerText);
                            }
                            switch(node.SelectSingleNode("Style").InnerText.ToLower()){
                                case "image":
                                    nbutton.DisplayStyle = ToolStripItemDisplayStyle.Image;
                                    addimage(nbutton,node.SelectSingleNode("Image").InnerText);
                                    if (node.SelectNodes("Text").Count > 0)
                                    {
                                        addtext(nbutton, node.SelectSingleNode("Text").InnerText);
                                    }
                                    break;
                                case "imageandtext":
                                    nbutton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                                    addimage(nbutton, node.SelectSingleNode("Image").InnerText);
                                    addtext(nbutton,node.SelectSingleNode("Text").InnerText);
                                    break;
                                default:
                                    nbutton.DisplayStyle = ToolStripItemDisplayStyle.Text;
                                    addtext(nbutton,node.SelectSingleNode("Text").InnerText);
                                    break;
                            }
                           
                            break;
                        case "label":
                            ToolBarLabelX nlabel = new ToolBarLabelX();
                            nToolbar.Items.Add(nlabel);
                            switch (node.SelectSingleNode("Style").InnerText.ToLower())
                            {
                                case "image":
                                    nlabel.DisplayStyle = ToolStripItemDisplayStyle.Image;
                                    addimage(nlabel, node.SelectSingleNode("Image").InnerText);
                                    break;
                                case "imageandtext":
                                    nlabel.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                                    addimage(nlabel, node.SelectSingleNode("Image").InnerText);
                                    addtext(nlabel, node.SelectSingleNode("Text").InnerText);
                                    break;
                                default:
                                    nlabel.DisplayStyle = ToolStripItemDisplayStyle.Text;
                                    addtext(nlabel, node.SelectSingleNode("Text").InnerText);
                                    break;
                            }
                            break;
                        case "dropdown":
                            MenuItemX nDropdown = new MenuItemX();
                            nToolbar.Items.Add(nDropdown);
                            //if (node.SelectNodes("Description").Count != 0)
                            //{
                            //    adddescription(nDropdown, node.SelectSingleNode("Description").InnerText);
                            //}
                            switch (node.SelectSingleNode("Style").InnerText.ToLower())
                            {
                                case "image":
                                    nDropdown.DisplayStyle = ToolStripItemDisplayStyle.Image;
                                    addimage(nDropdown, node.SelectSingleNode("Image").InnerText);
                                    break;
                                case "imageandtext":
                                    nDropdown.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                                    addimage(nDropdown, node.SelectSingleNode("Image").InnerText);
                                    addtext(nDropdown, node.SelectSingleNode("Text").InnerText);
                                    break;
                                default:
                                    nDropdown.DisplayStyle = ToolStripItemDisplayStyle.Text;
                                    addtext(nDropdown, node.SelectSingleNode("Text").InnerText);
                                    break;
                            }
                            foreach (XmlNode subnode in node.SelectNodes("Subitem"))
                            {
                                switch (subnode.SelectSingleNode("Type").InnerText.ToLower())
                                {
                                    case "button":
                                        MenuItemX nsubButton = new MenuItemX();
                                        nDropdown.DropDownItems.Add(nsubButton);
                                        //if (subnode.SelectNodes("Description").Count != 0)
                                        //{
                                        //    adddescription(nsubButton, subnode.SelectSingleNode("Description").InnerText);
                                        //}
                                        if (subnode.SelectNodes("Code").Count != 0)
                                        {
                                            addcode(nsubButton, subnode.SelectSingleNode("Code").InnerText);
                                        }
                                        switch (subnode.SelectSingleNode("Style").InnerText.ToLower())
                                        {
                                            case "image":
                                                nsubButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
                                                addimage(nsubButton, subnode.SelectSingleNode("Image").InnerText);
                                                break;
                                            case "imageandtext":
                                                nsubButton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                                                addimage(nsubButton, subnode.SelectSingleNode("Image").InnerText);
                                                addtext(nsubButton, subnode.SelectSingleNode("Text").InnerText);
                                                break;
                                            default:
                                                nsubButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
                                                addtext(nsubButton, subnode.SelectSingleNode("Text").InnerText);
                                                break;
                                        }
                                        break;
                                    case "label":
                                        ToolBarLabelX nsubLabel = new ToolBarLabelX();
                                        nDropdown.DropDownItems.Add(nsubLabel);
                                        //if (subnode.SelectNodes("Description").Count != 0)
                                        //{
                                        //    adddescription(nsubLabel, subnode.SelectSingleNode("Description").InnerText);
                                        //}
                                        switch (subnode.SelectSingleNode("Style").InnerText.ToLower())
                                        {
                                            case "image":
                                                nsubLabel.DisplayStyle = ToolStripItemDisplayStyle.Image;
                                                addimage(nsubLabel, subnode.SelectSingleNode("Image").InnerText);
                                                break;
                                            case "imageandtext":
                                                nsubLabel.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                                                addimage(nsubLabel, subnode.SelectSingleNode("Image").InnerText);
                                                addtext(nsubLabel, subnode.SelectSingleNode("Text").InnerText);
                                                break;
                                            default:
                                                nsubLabel.DisplayStyle = ToolStripItemDisplayStyle.Text;
                                                addtext(nsubLabel, subnode.SelectSingleNode("Text").InnerText);
                                                break;
                                        }
                                        break;
                                    case "spectator":
                                        ToolStripSeparator nsubSpectator = new ToolStripSeparator();
                                        nDropdown.DropDownItems.Add(nsubSpectator);
                                        break;
                                }
                            }
                            break;
                        case "spectator":
                            ToolStripSeparator nSpectator = new ToolStripSeparator();
                            nToolbar.Items.Add(nSpectator);
                            break;
                    }
                }
            }
        }
        /// <summary>
        /// Reads an toolbar file and returns the properties as array (Name,Author,Version,Description)
        /// </summary>
        /// <returns>String array (Name,Author,Version,Description)</returns>
        /// <param name="path">Toolbar file path</param>
        public static string[] hashToolbar(string path)
        {
            string[] ret;
            try
            {
                XmlDocument xmlFile = new XmlDocument();
                xmlFile.Load(path);
                ret = new string[4];
                ret[0] = xmlFile.SelectSingleNode("Toolbar/Information/Name").InnerText;
                ret[1] = xmlFile.SelectSingleNode("Toolbar/Information/Author").InnerText;
                ret[2] = xmlFile.SelectSingleNode("Toolbar/Information/Version").InnerText;
                ret[3] = xmlFile.SelectSingleNode("Toolbar/Information/Description").InnerText;
            }
            catch (Exception)
            {
                ret = null;
            }
            return ret;
        }
    }
}
